package com.example.bysj.Dao;
//201902104061徐中堂
import com.example.bysj.domain.GraduateProjectSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
//声明数据库访问类
@Repository("graduateProjectSourceDao")
public class GraduateProjectSourceDao {
    //自动注入
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //增加
    public boolean add(GraduateProjectSource graduateProjectSource) {
        String sql = "INSERT INTO GraduateProjectSource (no,description,remarks) VALUES (?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, graduateProjectSource.getNo(),
                graduateProjectSource.getDescription(),graduateProjectSource.getRemarks());
        if (affectedRowNum != 1) {
            return false;
        } else {

            return true;
        }
    }
    //根据id查找
    public GraduateProjectSource find(int id){
        String sql = "SELECT * FROM GraduateProjectSource WHERE id = ?";
        List<GraduateProjectSource> graduateProjectSources = jdbcTemplate.query(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(GraduateProjectSource.class));
        if(!graduateProjectSources.isEmpty()){
            return graduateProjectSources.get(0);
        } else
            return null;
    }
    public List<GraduateProjectSource> findAll(){
        String sql = "SELECT * FROM GraduateProjectSource";

        List<GraduateProjectSource> graduateProjectSources =
                jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(GraduateProjectSource.class){

                });
        return graduateProjectSources;
    }
    public boolean update(GraduateProjectSource graduateProjectSource){
        String sql = "UPDATE GraduateProjectSource SET no = ? ,description=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql,graduateProjectSource.getNo(),
                graduateProjectSource.getDescription(),graduateProjectSource.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    public boolean delete(int id){
        String sql = "DELETE FROM GraduateProjectSource WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}